package com.example.fileserver_API.Interfaces.Service.Board;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bestvike.linq.Linq;
import com.example.fileserver_API.Dao.SysappsettingMappers;
import com.example.fileserver_API.Dao.UploadpicturesMappers;
import com.example.fileserver_API.Entity.PO.sysappsettings;
import com.example.fileserver_API.Entity.PO.uploadpictures;
import com.example.fileserver_API.Entity.VO.APIResult;
import com.example.fileserver_API.Entity.VO.OutArgs.fileCounts;
import com.example.fileserver_API.Entity.VO.OutArgs.filestics;
import com.example.fileserver_API.Interfaces.IService.Board.IBoardService;
import com.example.fileserver_API.utils.APIResultUtils;
import com.example.fileserver_API.utils.GetDateUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.File;
import java.util.*;

@Service
public class BoardService implements IBoardService {
    @Resource
    SysappsettingMappers _sysappsettingMappers;
    @Resource
    UploadpicturesMappers uploadpicturesMappers;

    /**
     * 获取上传文件的所有父类文件夹
     * @return APIResult
     */
    @Override
    public APIResult GetFiles() {
        // 获取文件夹地址
        QueryWrapper<sysappsettings> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("appkey","path");
        sysappsettings syssettingvalue = _sysappsettingMappers.selectOne(queryWrapper);
        if (syssettingvalue==null)
            return APIResultUtils.error("未获取到文件路径配置！");
        System.out.println(syssettingvalue.getAppvalue());
        String path = syssettingvalue.getAppvalue();
        File file = new File(path);
        List<String> filelist = new ArrayList<>();
        File[] files = file.listFiles();
        for (int i = 0; i < files.length; i++) {
            if (files[i].isDirectory()) {
                System.out.println("一级文件夹：" + files[i].getName());
                filelist.add(files[i].getName());
            }
        }
        return APIResultUtils.success("",filelist);
    }

    /**
     * 获取主文件中的所有文件排除日期分类的文件夹
     * @return APIResult
     */
    @Override
    public APIResult GetFileStatiStics(){
        // 文件汇总
        List<fileCounts>  fileCounts = uploadpicturesMappers.GetGroupFileType();
        // 文件当日上传统计
        List<filestics> FileToday = uploadpicturesMappers.GetGroupFileToday();
        for (fileCounts item:fileCounts) {
            filestics file = Linq.of(FileToday).where(x->x.getFiletype().equals(item.getFiletype())).firstOrDefault();
            if (file!=null){
                item.setToday(file.getTodaycount());
            }
        }
        return APIResultUtils.success("",fileCounts);
    }

    /**
     * 获取月统计上传数量
     * @return APIResult
     */
    @Override
    public APIResult GetMouthCount() {
        List<fileCounts> fileCounts = uploadpicturesMappers.GetMouthCount();
        List<String> dataall = GetDateUtils.GetAllDate();
        List<fileCounts> data = new ArrayList<>();
        for (String item:dataall) {
            if (!fileCounts.isEmpty()){
                Optional<fileCounts> result = fileCounts.stream().filter(x -> x.getCreatedata().equals(item)).findFirst();
                if (result.isPresent()) {
                    data.add(result.get());
                } else {
                    fileCounts fileCounts2 = new fileCounts();
                    fileCounts2.setCreatedata(item);
                    fileCounts2.setFilecount(0);
                    data.add(fileCounts2);
                }
            }
            else{
                fileCounts fileCounts2 = new fileCounts();
                fileCounts2.setCreatedata(item);
                fileCounts2.setFilecount(0);
                data.add(fileCounts2);
            }
        }
        return APIResultUtils.success("",data);
    }

    /**
     * 获取最新的30条上传记录
     * @return APIResult
     */
    @Override
    public APIResult GetHistory() {
        QueryWrapper<uploadpictures> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("createtime");
        IPage<uploadpictures> iPage = new Page<>(1,30);
        List<uploadpictures> data = uploadpicturesMappers.selectPage(iPage,queryWrapper).getRecords();
        return APIResultUtils.success("",data);
    }

    /**
     * 获取一星期之内的所有文件上传情况
     * @return APIResult
     */
    @Override
    public APIResult GetWeekInfo() {
        // 一个星期的时间
        List<String> GetWeekDate = GetDateUtils.GetWeekDate();
        // 使用时间和类型进行分类
        List<fileCounts> fileCounts = uploadpicturesMappers.newGetMouthCount();
        List<String> filetype = Linq.of(fileCounts).groupBy(x -> x.getFiletype()).select(x -> x.getKey()).toList();
        List<List<fileCounts>> data = new ArrayList<>();
        for (String itemtype : filetype) {
            List<fileCounts> dataitem = new ArrayList<>();
            for (String item : GetWeekDate) {
                fileCounts file = Linq.of(fileCounts).firstOrDefault(x->x.getCreatedata().equals(item) && x.getFiletype().equals(itemtype));
                if (file==null){
                    fileCounts fileCounts2 = new fileCounts();
                    fileCounts2.setCreatedata(item);
                    fileCounts2.setFilecount(0);
                    fileCounts2.setFiletype(itemtype);
                    dataitem.add(fileCounts2);
                }
                else{
                    dataitem.add(file);
                }
            }
            data.add(dataitem);
        }
        return APIResultUtils.success("", data);
    }

    /**
     * 根据文件的后缀名进行统计
     * @return APIResult
     */
    @Override
    public APIResult GetSuffixCount() {
        List<fileCounts> data = uploadpicturesMappers.GetSuffixCount();
        return APIResultUtils.success("",data);
    }

    /**
     * 文件月度总汇
     * @return APIResult
     */
    @Override
    public APIResult GetMonthlyCount() {
        List<fileCounts> data = uploadpicturesMappers.GetMonthlyCount();
        List<fileCounts> dataitem = new ArrayList<>();
        for (Integer i = 1;i<=12;i++){
            String s = i.toString();
            fileCounts item = Linq.of(data).firstOrDefault(x->x.getCreatedata().equals(s));
            if (item==null){
                fileCounts sitem = new fileCounts();
                sitem.setCreatedata(s);
                sitem.setFilecount(0);
                dataitem.add(sitem);
            }else{
                dataitem.add(item);
            }
        }
        return APIResultUtils.success("",dataitem);
    }
}
